package mylibrary.activeMQ;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class Test {

	public static void main(String[] args) throws Exception {
		
		System.out.println(123);
		
		Test test = new Test();
		test.test1();
	}
	
	public void test() throws Exception {
		
		String url = "tcp://localhost:61616";
		String user = null;
		String password = null;
		String topic = "TestTopic";
		
		new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber1").start();
		new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber2").start();
		new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber3").start();
		new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber4").start();
		
		Thread.sleep(5000);

		new Thread(new MessagePublisher(topic,url,user,password), "Name-Publisher").start();
		
	}
	
	
	public void test1() throws Exception {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection connection = factory.createConnection();
		connection.start();
		
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		TextMessage msg = session.createTextMessage();
		msg.setText("Hello everyone");
		
		Queue destination = new ActiveMQQueue("");
		
//		Destination destination = session.createQueue("test-Queue");
//		Destination destination = session.createTopic("test-Topic");
		
		
//		MessageProducer sendPublisher = session.createProducer(destination);
//		sendPublisher.send(msg);
		
		
		MessageConsumer receiver1 = session.createConsumer(destination);
		receiver1.setMessageListener(new MessageListener() {
			public void onMessage(Message msg) {
				if(msg instanceof TextMessage) {
					try {
						System.out.println("1111111" + ((TextMessage)msg).getText());
						
						System.out.println("abc " + msg.getJMSRedelivered());
						
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		MessageConsumer receiver2 = session.createConsumer(destination);
		receiver2.setMessageListener(new MessageListener() {
			public void onMessage(Message msg) {
				if(msg instanceof TextMessage) {
					try {
						System.out.println("2222222" + ((TextMessage)msg).getText());
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		
		MessageProducer producer = session.createProducer(destination);
		
		for (int i = 0; i < 5; i++) {
			producer.send(session.createTextMessage("Message:" + i));
		}
		
		
//		MessageConsumer receiver2 = session.createConsumer(destination);
//		if(msg1 instanceof TextMessage) {
//			try {
//				System.out.println("2222222" + ((TextMessage)msg1).getText());
//			} catch (JMSException e) {
//				e.printStackTrace();
//			}
//		}
		
//		for (int i = 5; i < 10; i++) {
//			producer.send(session.createTextMessage("Message:" + i));
//		}
		
//		Message receiveMsg = receiver.receive();
//		System.out.println(receiveMsg);
//		if(receiveMsg instanceof TextMessage) {
//			System.out.println(((TextMessage)receiveMsg).getText());
//		}
		
		
//		Session anotherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//		MessageProducer anotherSendPublisher = anotherSession.createProducer(destination);
//		
//		msg = session.createTextMessage();
//		msg.setText("Hello lady");
//		anotherSendPublisher.send(msg);
//		MessageConsumer anotherReceiver = session.createConsumer(destination);
//		Message anotherMsg = anotherReceiver.receive();
//		System.out.println(anotherMsg);
	}
	
}
